﻿using CasignDesignGA.Casign;
using CasignDesignGA.Casing;
using CasignDesingGA.Casing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CasignDesignGA.Engine.Mutation
{
    class LongitudeMutationAlgorithm: IMutationAlgorithm
    {
        private static LongitudeMutationAlgorithm instance;

        private LongitudeMutationAlgorithm() { }

        public static LongitudeMutationAlgorithm Instance
        {
           get 
           {
              if (instance == null)
               {
                   instance = new LongitudeMutationAlgorithm();
               }
               return instance;
             }
         }

        public ISelectable mutate(ISelectable individual)
        {
            CasingUnit firstCasing = ((CasingDesign)individual).CasingCollection[0];
            CasingUnit secondCasing = ((CasingDesign)individual).CasingCollection[1];
            Random randomGenerator = new Random(DateTime.Today.Millisecond);
            double ran = randomGenerator.NextDouble();
            
            if (firstCasing.Longitude > secondCasing.Longitude)
            {
                long changedLongitude = Convert.ToInt64(ran * firstCasing.Longitude);
                firstCasing.Longitude = changedLongitude;
                secondCasing.Longitude = Well.Well.Instance.Depth - changedLongitude;
            }
            else
            {
                long changedLongitude = Convert.ToInt64(ran * secondCasing.Longitude);
                secondCasing.Longitude = changedLongitude;
                firstCasing.Longitude = Well.Well.Instance.Depth - changedLongitude;
            }

            return individual;
        }
    }
}
